from fastapi.requests import Request
from pydantic import BaseModel

from utils.product_collector import CLASSIFY_MAP, ProductCollector
from utils.response import json_response


async def get_classify(request: Request):
    return json_response(
        request_id=request.state.request_id,
        data=[{'id': key, 'name': value} for key, value in CLASSIFY_MAP.items()]
    )


class Params(BaseModel):
    classify: int
    url: str
    checkChinese: bool = True

async def add_product(request: Request, params: Params):
    pc = ProductCollector()
    try:
        data = await pc.add_product(params.url, params.classify, params.checkChinese, True)
    except Exception as e:
        return json_response(
            request_id=request.state.request_id,
            status_code=500,
            message=str(e)
        )

    return json_response(
        request_id=request.state.request_id,
        data=data
    )
